//
//  TrafficSignalTimer.m
//  Portable Traffic Signal Assistor
//
//  Created by Hao Xu on 4/5/12.
//  Copyright (c) 2012 TTU. All rights reserved.
//

#import "TrafficSignalTimer.h"

@implementation TrafficSignalTimer

+ (NSString *) getCurrentTimeString
{
    NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone];
    NSDate *currentDateTime = [NSDate date]; 
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setTimeZone:localTimeZone];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateInStringFormated = [dateFormatter stringFromDate:currentDateTime];
    NSLog(@"%@", dateInStringFormated);
//    NSDate *currentDateTime_Zone=[dateFormatter dateFromString:dateInStringFormated];
//    NSString *dateOnlyString;
    NSString *timeOnlyString=nil;
    NSRange range = [dateInStringFormated rangeOfString:@" "];
    if (range.location != NSNotFound) {
        
//        dateOnlyString = [dateInStringFormated substringToIndex:(range.location)];
        timeOnlyString=[dateInStringFormated substringFromIndex:(range.location)];
        
    }
    return timeOnlyString;
    
}
+ (NSString *) getMasterTimeString:(NSString *)standardTimeOffset
{
    NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone];
    NSDate *currentDateTime = [NSDate date]; 
    NSDate *agencyTime=[currentDateTime dateByAddingTimeInterval:[standardTimeOffset intValue]];
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setTimeZone:localTimeZone];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateInStringFormated = [dateFormatter stringFromDate:agencyTime];
    NSLog(@"%@", dateInStringFormated);
    //    NSDate *currentDateTime_Zone=[dateFormatter dateFromString:dateInStringFormated];
//    NSString *dateOnlyString;
    NSString *timeOnlyString=nil;
    NSRange range = [dateInStringFormated rangeOfString:@" "];
    if (range.location != NSNotFound) {
        
//        dateOnlyString = [dateInStringFormated substringToIndex:(range.location)];
        timeOnlyString=[dateInStringFormated substringFromIndex:(range.location)];
        
    }
    return timeOnlyString;
}
+ (int) calculateCycleTimer:(int) cycle time:(NSDate *)date offSet:(int)offSet syncTime:(NSDate *)syncTime standardTimeOff:(int)standardTimeOff
{
//    NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone];
    NSDate *currentDateTime = date;
    NSDate *agencyTime=[currentDateTime dateByAddingTimeInterval:standardTimeOff];
//    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
//    [dateFormatter setTimeZone:localTimeZone];
//    [dateFormatter setDateFormat:@"HH"];
//    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
//    NSString *dateOnlyString=[dateFormatter stringFromDate:agencyTime];
//    NSString * initialTimeString=[[dateOnlyString stringByAppendingString:@" "] stringByAppendingString:syncTime];
//    NSLog(@"%@", initialTimeString);
//    
//    NSDateFormatter *inputFormat = [[[NSDateFormatter alloc] init] autorelease];
//    [inputFormat setTimeZone:localTimeZone];
//    [inputFormat setDateFormat:@"yyyy-MM-dd hh:mm aa"];
//    NSDate *syncDate = [inputFormat dateFromString: initialTimeString];
    
    if ([agencyTime compare:syncTime]==NSOrderedAscending) {
        NSLog(@"date1 is earlier than date2");
        syncTime=[syncTime dateByAddingTimeInterval:(-3600*24)];
    }
    NSInteger secondsBetweenTimes =[agencyTime timeIntervalSinceDate:syncTime];
    if (cycle==0) {
        return 0;
    }
    int cycleTimer=(secondsBetweenTimes+(cycle-offSet))%cycle;
    if (cycleTimer==0) {
        cycleTimer=cycle;
    }
    return cycleTimer;
    
}



@end
